import json
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.urls import reverse
from django.views.decorators.csrf import csrf_exempt
from login.models import Publisher, Receiver, Staff
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
# Create your views here.


def show_login(req):
    return render(req, 'accounts/login.html', {"page_state": 'sign'})


def staff_login(req):
    return render(req, 'accounts/staff_login.html', {"page_state": 'login'})


def register(request):
    context = dict()
    context['page_state'] = 'sign'

    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        identity = request.POST.get('sign_user_type')

        # 创建新登录用户
        user = User.objects.create_user(username=username, password=password)
        user.extension.identity = identity
        user.save()
        if identity == 'publisher':
            pub_list = Publisher.objects.all()
            pub_username = []
            for i in pub_list:
                pub_username.append(i.pub_username)
            if username not in pub_username:
                new_user = Publisher(pub_username=username, password=password)
                new_user.save()
                context['sign_result'] = 'success'
                context['msg'] = '注册成功'
            else:
                context['sign_result'] = 'error'
                context['msg'] = '用户名已存在'
            return render(request, 'accounts/login.html', context)
        elif identity == 'receiver':
            rec_list = Receiver.objects.all()
            rec_username = []
            for i in rec_list:
                rec_username.append(i.rec_username)
            if username not in rec_username:
                new_user = Receiver(rec_username=username, password=password)
                new_user.save()
                context['sign_result'] = 'success'
                context['msg'] = '注册成功'
            else:
                context['sign_result'] = 'error'
                context['msg'] = '用户名已存在'
            return render(request, 'accounts/login.html', context)
        elif identity == 'staff':
            staff_list = Staff.objects.all()
            staff_username = []
            for i in staff_list:
                staff_username.append(i.rec_username)
            if username not in staff_username:
                new_user = Staff(staff_num=username, password=password)
                new_user.save()
                context['sign_result'] = 'success'
                context['msg'] = '注册成功'
            else:
                context['sign_result'] = 'error'
                context['msg'] = '该员工已存在'
            return render(request, 'accounts/staff_login.html', context)
    return render(request, 'accounts/login.html', context)


def user_login(request):
    context = dict()
    context["page_state"] = 'login'
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        identity = request.POST.get('login_user_type')
        context = dict()
        context["page_state"] = 'login'
        # print(username)
        # print(password)
        # print(identity)
        # 验证用户凭证
        user = authenticate(request, username=username, password=password)
        if user is not None:
            login(request, user)
            # 登录成功后跳转到主页
            if identity == 'publisher':
                return redirect(reverse('pubSys:show_index'))
            elif identity == 'receiver':
                return redirect(reverse('recSys:show_index'))
            elif identity == 'staff':
                return redirect(reverse('staffSys:show_index'))
            else:
                return render(request, 'accounts/login.html', {})
        else:
            context['login_result'] = 'error'
            context['msg'] = '用户名或密码错误'
            if identity == "staff":
                return render(request, 'accounts/staff_login.html', context)
            else:
                return render(request, 'accounts/login.html', context)
    else:
        return render(request, 'accounts/login.html', context)  # 显示登录表单


def user_logout(request):
    # 这里还要根据用户类别退出到不同的页面
    logout(request)
    return redirect('home')  # 退出后跳转到主页

